/**
 *
 */
package com.yangtao.framework.common;

import com.yangtao.framework.dojo.DojoDataModel;

import java.io.Serializable;
import java.util.Collection;

/**
 * 与具体ORM实现无关的分页参数及查询结果封装. 注意所有序号从1开始.
 *
 * @author 杨涛
 */
public class Pagination extends DojoDataModel implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 排序字段名称
	 */
	private String sortField;
	/**
	 * 排序方向
	 */
	private String sortOrder;
	/**
	 * 查询字符串
	 */
	private String queryString;
	/**
	 * 是否自动分页
	 */
	private boolean autoCount = true;
	/**
	 * 每页记录数
	 */
	private int pageSize = 10;
	/**
	 * 页码号
	 */
	private int pageNo = 1;
	/**
	 * 起始记录数 默认-1
	 */
	private int start = -1;
	/**
	 * 最大记录数 默认-1
	 */
	private int count = -1;

	public String getSortField() {
		return sortField;
	}

	public void setSortField(String sortField) {
		this.sortField = sortField;
	}

	public String getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}

	/**
	 * @return the queryString
	 */
	public String getQueryString() {
		return queryString;
	}

	/**
	 * @param queryString the queryString to set
	 */
	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public boolean isAutoCount() {
		return autoCount;
	}

	public void setAutoCount(boolean autoCount) {
		this.autoCount = autoCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public Collection getRows() {
		return super.getItems();
	}

	public long getTotalCount() {
		return super.getNumRows();
	}

	public long getTotalPage() {
		return getTotalCount() % pageSize == 0 ? getTotalCount() / pageSize : getTotalCount() / pageSize + 1;
	}

	public int getStart() {
		if (start > 0)
			return start;
		return (pageNo - 1) * pageSize;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getCount() {
		if (count > 0)
			return count;
		return pageSize;
	}

	public void setCount(int count) {
		this.count = count;
	}
}
